package com.eminem.dao;

import com.eminem.common.StrMath;
import com.eminem.dao.base.BaseDao;
import com.eminem.model.User;
import com.eminem.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao<User> {

    public UserDao() {
        super(User.class);
    }

    public Map<String, Object> findUserBaseInfo(String username) {
        Map<String,Object> map = null;
        String sql = "select u.id as userId,u.username,ui.real_name as realName,ui.pic,d.name as deptName\n" +
                "from user u\n" +
                "left join user_info ui on u.id = ui.user_id\n" +
                "left join dept d on u.dept_id = d.id\n" +
                "where username = ? ";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql, new MapHandler(), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return map;
    }

    public Map<String, Object> getUserInfo(String id) {
        Map<String,Object> map = null;
        String sql = "select d.name as deptname,u.*,ui.*,(select count(*) as total from user_focus where user_focus_id = ?) as total_focus\n" +
                "from user u\n" +
                "left join user_info ui on u.id = ui.user_id\n" +
                "left join dept d on u.dept_id = d.id\n" +
                "where u.id = ?";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql, new MapHandler(), id,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return map;
    }

    public List<Map<String, Object>> findUserList(String userId, int currentPage, String names) {
        String sql = "select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,(select id from user_focus where user_id = ? and user_focus_id = ui.user_id) as fid\n" +
                "from user_info ui limit ?,?";

        if(null != names){
            sql = "select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,(select id from user_focus where user_id = ? and user_focus_id = ui.user_id) as fid\n" +
                    "from user_info ui\n" +
                    "where ui.real_name like ?\n" +
                    "limit ?,?";
        }
        List<Map<String,Object>> mapList = new ArrayList<>();
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != names){
                mapList = qr.query(sql, new MapListHandler(), userId,names+"%",(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }else {
                mapList = qr.query(sql, new MapListHandler(), userId,(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
//                System.out.println((currentPage - 1) * StrMath.PAGE_SIZE);
//                System.out.println(StrMath.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mapList;
    }

    public long getUserTotal(String name) {
        long count = 0L;
        String sql = "select count(*) as CountUser from user_info";
        if (null != name){
            sql = "select count(*) as CountUser from user_info where real_name like ?";
        }
        Map<String, Object> map = null;
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null == name){
                map = qr.query(sql,new MapHandler());
            }else {
                map = qr.query(sql,new MapHandler(),name+"%");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            count = Long.parseLong(map.get("CountUser").toString());
        }
        return count;
    }

    public List<Map<String, Object>> findUserName(String userId,int currentPage) {
        List<Map<String, Object>> map = new ArrayList<>();
        String sql = "select uf.user_id,uf.user_focus_id,ui.real_name from user_focus uf\n" +
                "left join user u on u.id = uf.user_id\n" +
                "left join user_info ui on ui.user_id = uf.user_focus_id\n" +
                "where u.id = ?\n"+
                "limit ?,?";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql,new MapListHandler(),userId,(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            return map;
        }
        return null;
    }

    public long getFollowCount(String userId) {
        long count = 0L;
        String sql = "select count(*) as countUser from user_focus uf\n" +
                "where uf.user_id = ?";
        Map<String, Object> map = null;
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql,new MapHandler(),userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            count = Long.parseLong(map.get("countUser").toString());
        }
        return count;
    }


//    public List<User> FindName(String username){
//        String sql = "select * from user where username = ?";
//        List<User> userList = new ArrayList<>();
//        try {
////            QueryRunner qr = new QueryRunner((DataSource) JDBCUtil.getConnection());
//            QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
//            userList = qr.query(sql,new BeanListHandler<>(User.class),username);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return userList;
//    }
//
//    public List<User> findEmail(String key, String value) {
//        List<User> userList = new ArrayList<>();
//        String sql = "select * from user where "+key+" = ?";
//        try {
//            QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
//            userList = qr.query(sql,new BeanListHandler<>(User.class),value);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return userList;
//    }
//
//    public List<User> findByCols(String cond, Map<String,Object> map){
//        List<User> userList = new ArrayList<>();
//        StringBuffer sb = new StringBuffer();
//        sb.append("select * from user where");
//        if (null != map || map.isEmpty()){
//            sb.delete(sb.lastIndexOf("where"),sb.length());
//        }else {
//            Set<String> keySet = map.keySet();
//            for (String key : keySet) {
//                sb.append(key).append("= '").append(map.get(key)).append("' ").append(cond).append(" ");
//            }
//            sb.delete(sb.lastIndexOf(cond),sb.length());
//        }
//        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
//        try {
//            userList = qr.query(sb.toString(),new BeanListHandler<>(User.class));
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return userList;
//    }



}
